package cn.cjlmonster.demo02.ui;

import android.graphics.Color;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.leochuan.CenterSnapHelper;
import com.leochuan.ScaleLayoutManager;
import com.leochuan.ScrollHelper;

import java.util.ArrayList;
import java.util.List;

import cn.cjlmonster.demo02.R;
import cn.cjlmonster.demo02.adapter.CardAdapter;
import cn.cjlmonster.demo02.adapter.TabAdapter;
import cn.cjlmonster.demo02.base.BaseAppCompatActivity;
import cn.cjlmonster.demo02.decoration.GalleryItemDecoration;
import cn.cjlmonster.demo02.decoration.GradientIndicatorDecoration;
import cn.cjlmonster.demo02.decoration.LinePagerIndicatorDecoration;

public class GalleryActivity extends BaseAppCompatActivity implements TabAdapter.OnItemSelectedListener {

    @Override
    protected int getLayoutId() {
        return R.layout.activity_gallery;
    }

    @Override
    protected boolean canBack() {
        return true;
    }

    private int mScreenWidth;
    private static final float MIN_SCALE = 0.95f;
    private static final float MAX_SCALE = 1.15f;

    private RecyclerView rv_tabs;
    private RecyclerView rv_data;

    private List<Integer> mColors;
    private CardAdapter cardAdapter;

    @Override
    protected void initView() {

        mScreenWidth = getResources().getDisplayMetrics().widthPixels;

        rv_tabs = findViewById(R.id.rv_tabs);
        rv_data = findViewById(R.id.rv_data);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(RecyclerView.HORIZONTAL);
        rv_tabs.setLayoutManager(layoutManager);

        String[] mTabs = {"tab1", "tab2", "tab3"};

        TabAdapter tabAdapter = new TabAdapter(this, mTabs, this);
        rv_tabs.setAdapter(tabAdapter);

//        LinearLayoutManager layoutManager1 = new LinearLayoutManager(this);
//        layoutManager1.setOrientation(RecyclerView.HORIZONTAL);
//        rv_data.setLayoutManager(layoutManager1);
//
//
//        PagerSnapHelper snapHelper = new PagerSnapHelper();
//        snapHelper.attachToRecyclerView(rv_data);

        ScaleLayoutManager layoutManager1 = new ScaleLayoutManager
                .Builder(this, 50)
                .setMoveSpeed(0.8f)
                .build();
        layoutManager1.setMeasurementCacheEnabled(false);
        rv_data.setLayoutManager(layoutManager1);


        new CenterSnapHelper().attachToRecyclerView(rv_data);

        mColors = new ArrayList<>();

        cardAdapter = new CardAdapter(this, mColors);

        rv_data.setAdapter(cardAdapter);

        rv_data.setItemAnimator(null);

//        rv_data.addItemDecoration(new GalleryItemDecoration(this));
        rv_data.addItemDecoration(new GradientIndicatorDecoration());

//        rv_data.addOnScrollListener(new RecyclerView.OnScrollListener() {
//
//            @Override
//            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
//                super.onScrolled(recyclerView, dx, dy);
//                calScaleItem(recyclerView);
//            }
//        });
        onItemSelected(0, null);
    }

    private void calScaleItem(RecyclerView recyclerView) {
        final int childCount = recyclerView.getChildCount();
        Log.i("测试123", "onScrolled: " + childCount);
        for (int i = 0; i < childCount; i++) {
            View child = recyclerView.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

            int left = child.getLeft();
            int right = mScreenWidth - child.getRight();
            final float percent = left < 0 || right < 0 ? 0 : Math.min(left, right) * 1f / Math.max(left, right);
            float scaleFactor = MIN_SCALE + Math.abs(percent) * (MAX_SCALE - MIN_SCALE);
            child.setLayoutParams(params);
            child.setScaleY(scaleFactor);
            child.setScaleX(scaleFactor);
        }
    }

    @Override
    public void onItemSelected(int position, String tab) {
        int count = mColors.size();
        mColors.clear();
        cardAdapter.notifyItemRangeRemoved(0, count);
        switch (position) {
            case 0:
                mColors.add(Color.RED);
                mColors.add(Color.BLUE);
                mColors.add(Color.GREEN);
                cardAdapter.notifyItemRangeInserted(0, mColors.size());
                break;
            case 1:
                mColors.add(Color.MAGENTA);
                mColors.add(Color.GRAY);
                cardAdapter.notifyItemRangeInserted(0, mColors.size());
                break;
            case 2:
                mColors.add(Color.CYAN);
                cardAdapter.notifyItemRangeInserted(0, mColors.size());
                break;
        }
        rv_data.scrollToPosition(0);
    }
}